package com.opo5.logisticsmanagement.controller;

import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.AutoTask;
import com.opo5.logisticsmanagement.domain.Dict;
import com.opo5.logisticsmanagement.domain.Menu;
import com.opo5.logisticsmanagement.service.AutoTaskService;
import com.opo5.logisticsmanagement.service.DictService;
import com.opo5.logisticsmanagement.service.MenuService;
import com.opo5.logisticsmanagement.service.MessageService;
import com.opo5.logisticsmanagement.validationgroup.InsertValida;
import com.opo5.logisticsmanagement.validationgroup.UpdateValida;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Title: SystemManageController
 * @Author: lixin
 * @Date: 2022/07/11/09:17
 * @Description: 系统管理
 */
@RestController
@RequestMapping("/system")
@CrossOrigin
public class SystemManageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private AutoTaskService autoTaskService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private DictService dictService;

    /**
     * @Author: lixin
     * @Description: 按条件查询所有消息信息
     * @param title 消息标题
     */
    @GetMapping(value={
            "/getMessageByCondition/{title}",
            "/getMessageByCondition"
    })
    public R getMessageByCondition(@PathVariable(value="title",required = false) String title){
        R r = messageService.getMessageByCondition(title);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 修改消息信息状态
     * @param id 消息的id
     */
    @PutMapping("/editMessage")
    public R editMessage(@RequestBody @NotNull(message = "id不能为空") Integer id){
        R r = messageService.editMessage(id);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 修改所有消息信息状态
     */
    @PutMapping("/editAllMessage")
    public R editAllMessage(){
        R r = messageService.editAllMessage();
        return r;
    }



    /**
     * @Author: lixin
     * @Description: 按条件查询所有自动任务
     * @param reference 类名
     * @param status 状态
     * @param currentPage 当前页
     * @param pageSize 几条数据
     */
    @GetMapping(value={
            "/getUserByCondition/{reference}/{status}/{currentPage}/{pageSize}",
            "/getUserByCondition/{currentPage}/{pageSize}",
            "/getUserByCondition/only/reference/{reference}/{currentPage}/{pageSize}",
            "/getUserByCondition/only/status/{status}/{currentPage}/{pageSize}"
    })
    public R getAutoTaskByCondition(@PathVariable(value = "reference",required = false) String reference,@PathVariable(value = "status",required = false)String status,@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        R r = autoTaskService.getAutoTaskByCondition(reference,status,currentPage,pageSize);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 添加自动任务
     * @param autoTask 自动任务
     */
    @PostMapping("/addAutoTask")
    public R addAutoTask(@RequestBody @Validated(InsertValida.class) AutoTask autoTask){
        R r = autoTaskService.addAutoTask(autoTask);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 修改自动任务
     * @param autoTask 自动任务
     */
    @PutMapping("/editAutoTask")
    public R editAutoTask(@RequestBody @Validated(UpdateValida.class) AutoTask autoTask){
        R r = autoTaskService.editAutoTask(autoTask);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 开关自动任务
     * @param id 自动任务id
     */
    @PutMapping("/switchAutoTaskById")
    public R switchAutoTaskById(@RequestBody @NotNull(message = "id不能为空") Integer id){
        R r = autoTaskService.switchAutoTaskById(id);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 删除自动任务
     * @param ids 多个自动任务id
     */
    @DeleteMapping("/delAutoTask")
    public R delAutoTask(@RequestBody @NotEmpty(message = "ids不能为空") Integer[] ids){
        R r = autoTaskService.delAutoTask(ids);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 获取菜单
     * @param id role的id
     */
    @GetMapping(value={"/getMenu/{id}","/getMenu"})
    public R getMenu(@PathVariable(required = false) Integer id){
        R r = menuService.getMenu(id);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 添加菜单
     * @param menu 菜单
     */
    @PostMapping("/addMenu")
    public R addMenu(@RequestBody @Validated(InsertValida.class) Menu menu){
        R r = menuService.addMenu(menu);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 修改菜单
     * @param menu 菜单
     */
    @PutMapping("/editMenu")
    public R editMenu(@RequestBody @Validated(UpdateValida.class) Menu menu){
        R r = menuService.editMenu(menu);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 删除菜单
     * @param id 菜单的id
     */
    @DeleteMapping("/delMenu")
    public R delMenu(@RequestBody @NotNull(message = "id不能为空") Integer id){
        R r = menuService.delMenu(id);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 按条件查询所有字典
     * @param text 类名
     * @param value 状态
     * @param currentPage 当前页
     * @param pageSize 几条数据
     */
    @GetMapping(value={
            "/getDictByCondition/{text}/{value}/{currentPage}/{pageSize}/{flag}",
            "/getDictByCondition/{currentPage}/{pageSize}/{flag}",
            "/getDictByCondition/only/text/{text}/{currentPage}/{pageSize}/{flag}",
            "/getDictByCondition/only/value/{value}/{currentPage}/{pageSize}{flag}"
    })
    public R getDictByCondition(@PathVariable(value = "text",required = false) String text,@PathVariable(value = "value",required = false)String value,@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize,@PathVariable("flag") Boolean flag){
        R r = dictService.getDictByCondition(text,value,currentPage,pageSize,flag);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 根据id查询字典
     * @param id
     */
    @GetMapping("/getDictById/{id}")
    public R getDictById(@PathVariable("id") @NotNull(message = "id不能为空") String id){
        R r = dictService.getDictById(id);
        return r;
    }


    /**
     * @Author: lixin
     * @Description: 添加字典
     * @param dict 字典
     */
    @PostMapping("/addDict")
    public R addDict(@RequestBody @Validated(InsertValida.class) Dict dict){
        R r = dictService.addDict(dict);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 删除字典
     * @param dict 字典
     */
    @PutMapping("/editDict")
    public R editDict(@RequestBody @Validated(UpdateValida.class) Dict dict){
        R r = dictService.editDict(dict);
        return r;
    }

    /**
     * @Author: lixin
     * @Description: 删除字典
     * @param id 字典的id
     */
    @DeleteMapping("/delDict")
    public R delDict(@RequestBody @NotNull(message = "id不能为空") Integer id){
        R r = dictService.delDict(id);
        return r;
    }

    @PutMapping("/switchDict")
    public R switchDict(@RequestBody @NotNull(message = "id不能为空") Integer id){
        R r = dictService.switchDict(id);
        return r;
    }




}
